﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IP2Client
{
    /// <summary>
    /// UserManagement form
    /// </summary>
    public partial class UserManagement : Form
    {
        /// <summary>
        /// UserManagement Constructor
        /// </summary>
        public UserManagement()
        {
            InitializeComponent();
        }

        /// <summary>
        ///  Method that gets called on creation of this form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PatientManagement_Load(object sender, EventArgs e)
        {
            this.CenterToScreen();          
        }

        /// <summary>
        /// Menu item which closes this form on click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {            
            this.Close();
        }

        /// <summary>
        /// Checks if all fields are filled and if they are a new user is created.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addUserButton_Click(object sender, EventArgs e)
        {
            //If there is no username entered in the username textbox
            if (usernameAddTextbox.Text == "")
            {
                MessageBox.Show("Enter a username before you can continue!", "Error"); return;                
            }
            //If there is no password entered in the password textbox
            if (passwordAddTextbox.Text == "")
            {
                MessageBox.Show("Enter a password before you can continue!", "Error"); return;                    
            }
            //If there is no option selected from the user rights combobox
            if (userRightsCombobox.SelectedIndex == -1)
            {
                MessageBox.Show("Select the user rights before you can continue!", "Error"); return;
            }
            //If the option Patient is selected from the user rights combobox
            if (userRightsCombobox.SelectedItem.ToString() == "Patient" && doctorTextBox.Text == "")
            {
                MessageBox.Show("Enter the corresponding doctor by the patient!", "Error"); return;
            }

            string username = usernameAddTextbox.Text;
            string password = passwordAddTextbox.Text;
            string userRights = userRightsCombobox.SelectedItem.ToString();
            string doctor = doctorTextBox.Text;
            //If there is no doctor entered
            if (doctor == "")
            {
                ConnectionHandler.SendObject("!addUser" + " " + username + " " + password + " " + userRights.ToUpper());
                
                usernameAddTextbox.Text = "";
                passwordAddTextbox.Text = "";
                userRightsCombobox.SelectedIndex = -1;
            }
            else
            {
                ConnectionHandler.SendObject("!addUser" + " " + username + " " + password + " " + userRights.ToUpper() + " " + doctor);

                usernameAddTextbox.Text = "";
                passwordAddTextbox.Text = "";
                userRightsCombobox.SelectedIndex = -1;
                doctorTextBox.Text = "";
            }            
        }

        /// <summary>
        /// Makes sure a user and doctor have seperate userRights and different user views
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void userRightsCombobox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (userRightsCombobox.SelectedIndex == -1)
            {
                return;
            }
            else
            {
                if (userRightsCombobox.SelectedItem.ToString() == "Patient")
                {
                    doctorTextBox.Enabled = true;
                }
                else
                {
                    doctorTextBox.Enabled = false;
                }
            }
        }
    }
}
